import { _decorator, Component, Node, Prefab, instantiate, Label, Button } from 'cc';
import { EventDispatcher } from '../../../../ngame/util/EventDispatcher';
import GameConst from '../../../GameConst';
const { ccclass, property } = _decorator;

export interface UISceneButton{
    title?:string,
    func:Function,
}

@ccclass('UISceneController')
export class UISceneController extends Component {

    @property({type:Prefab,displayName:"按钮"})
    btnPrefab:Prefab;

    isShow:boolean = false;

    onLoad(){
        this.clear();
        this.onEvent();
    }

    onEvent(){
        EventDispatcher.getIns().on(GameConst.TOUCH.TOUCH_WORLD_CLICK,this.clear,this);
    }

    offEvent(){
        EventDispatcher.getIns().off(GameConst.TOUCH.TOUCH_WORLD_CLICK,this.clear,this);
    }

    clear(){
        this.isShow = false;
        this.node.destroyAllChildren();
    }

    show(btns:UISceneButton[]){

        this.isShow = true;
        btns.forEach((btnInfo:UISceneButton) => {

            let btn = instantiate(this.btnPrefab);
            btn.getComponentInChildren(Label).string = btnInfo.title || "文本";
            btn.on(Button.EventType.CLICK,btnInfo.func);
            this.node.addChild(btn);

        });

    }

}

